package com.ruoyi.web.controller.app;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import com.ruoyi.app.service.IAppUserService;
import lombok.RequiredArgsConstructor;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;

import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.app.domain.vo.AppGroupUserVo;
import com.ruoyi.app.domain.bo.AppGroupUserBo;
import com.ruoyi.app.service.IAppGroupUserService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * APP群组
 *
 * @author heike
 * @date 2023-01-29
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/groupUser")
public class AppGroupUserController extends BaseController {

    private final IAppGroupUserService iAppGroupUserService;
    private final IAppUserService appUserService;

    /**
     * 查询APP群组列表
     */
    @SaCheckPermission("system:groupUser:page")
    @GetMapping("/page")
    public TableDataInfo<AppGroupUserVo> page(AppGroupUserBo bo, PageQuery pageQuery) {
        return iAppGroupUserService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询APP群组列表
     */
    @SaCheckPermission("system:groupUser:list")
    @GetMapping("/list")
    public R<List<AppGroupUserVo>> list(AppGroupUserBo bo) {
        List<AppGroupUserVo> appGroupUserVos = iAppGroupUserService.queryList(bo);
        appGroupUserVos.forEach(e -> e.setUserVo(appUserService.queryByIdUserVo(e.getUserId())));
        return R.ok(appGroupUserVos);
    }

    /**
     * 导出APP群组列表
     */
    @SaCheckPermission("system:groupUser:export")
    @Log(title = "APP群组", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(AppGroupUserBo bo, HttpServletResponse response) {
        List<AppGroupUserVo> list = iAppGroupUserService.queryList(bo);
        ExcelUtil.exportExcel(list, "APP群组", AppGroupUserVo.class, response);
    }

    /**
     * 获取APP群组详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:groupUser:query")
    @GetMapping("/{id}")
    public R<AppGroupUserVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iAppGroupUserService.queryById(id));
    }

    /**
     * 新增APP群组
     */
    @SaCheckPermission("system:groupUser:add")
    @Log(title = "APP群组", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody AppGroupUserBo bo) {
        return toAjax(iAppGroupUserService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改APP群组
     */
    @SaCheckPermission("system:groupUser:edit")
    @Log(title = "APP群组", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@RequestBody AppGroupUserBo bo) {
        return toAjax(iAppGroupUserService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除APP群组
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:groupUser:remove")
    @Log(title = "APP群组", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iAppGroupUserService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }
}
